package com.aierjun.myapplication;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;

import com.aierjun.ani.net.OkGo.okupload.UploadingActivity;
import com.aierjun.mainview.Sample.TestMainViewSample;
import com.aierjun.myapplication.utils.BitmapUtil;
import com.aierjun.myapplication.utils.FileUtils;
import com.aierjun.myapplication.utils.GetPathFromUri4kitkat;
import com.aierjun.myapplication.utils.MPermission;
import com.lzy.okgo.model.HttpParams;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private ImageView imageView;
    private int editSize = -1; //输入长度
    private EditAsyncTask editAsyncTask;
    private SurfaceView surfaceView;
    private Button start;
    private Button pause;
    private Button stop;

//    private PickerScrollView pickerScrollView;

    private NumberPicker numberPicker;


    List<String> options1Items;
    List<List<String>> options2Items;
    List<List<List<String>>> options3Items;

    private static MainActivity inStance;

    public static synchronized MainActivity Instance() {
        if (inStance == null)
            inStance = new MainActivity();
        return inStance;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                Bitmap b = (Bitmap) msg.obj;
                if (b != null)
                    imageView.setImageBitmap(b);
                Log.d("Jun", "..........." + b.getHeight() * b.getWidth());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inStance = this;
        setContentView(R.layout.activity_main);
        MPermission.with(this)
                .setPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS)
                .requestPermission();
        findView();
        initView();
    }

    @Override

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);

    }


    @MPermission.PermissionOK

    private void valdateSuccess() {

        Log.i("Jun", "权限通过");

    }


    @MPermission.PermissionFail

    public void validateFail() {

        Log.i("Jun", "没有相应的权限");

    }


    private void findView() {
        editText = (EditText) findViewById(R.id.editText);
        imageView = (ImageView) findViewById(R.id.imageView);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        start = (Button) findViewById(R.id.start);
        pause = (Button) findViewById(R.id.pause);
        stop = (Button) findViewById(R.id.stop);
        final MediaRecorder mediaRecorder = new MediaRecorder();
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MediaRecorderUtils.startVideo(MainActivity.this,MediaRecorderUtils.setSurfaceView(surfaceView),mediaRecorder);
                startActivity(new Intent(MainActivity.this, CarMessageAddActivity.class));
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MediaRecorderUtils.stopVideo(mediaRecorder);

//                startActivity(new Intent(MainActivity.this,CreditCommitActivity.class));
//                startIntent();

//                startActivity(new Intent(MainActivity.this,com.aierjun.tentcentmaplib.MainActivity.class));
                startActivity(new Intent(MainActivity.this,TestMainViewSample.class));
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, EvaluateActivity.class));
            }
        });

//        pickerScrollView = (PickerScrollView) findViewById(R.id.pickerScrollView);
//        pickerScrollView.setData(new String[]{"1","2","3","4","5","6","7"});

        numberPicker = (NumberPicker) findViewById(R.id.numberPicker);
        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            }
        });
        numberPicker.setOnScrollListener(new NumberPicker.OnScrollListener() {
            @Override
            public void onScrollStateChange(NumberPicker view, int scrollState) {

            }
        });
        numberPicker.setDisplayedValues(new String[]{"第一个", "第二个 ", "第三个", "第四个", "第五个", "第六个", "第七个"});
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(6);
    }

    private void initView() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap b = null;
                if (FileUtils.hasSDCard()) {
                    try {
                        handler.obtainMessage(0, BitmapUtil.rotateBitmapSize("http://img.my.csdn.net/uploads/201407/26/1406383290_9329.jpg", true, new BitmapUtil.FilePathCallBack() {
                            @Override
                            public void getPathFile(String path) {
                                Log.d("Jun path CallBack", path);
                            }
                        })).sendToTarget();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
//                        b.recycle();
                    }
                }
            }
        }).start();
//        editAsyncTask = new EditAsyncTask();
//        editAsyncTask.execute();
    }

    class EditAsyncTask extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            Log.d("Jun", "ssssss");
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            while (true) {
                try {
                    if (editText.getText().toString().length() != editSize) {
                        onProgressUpdate(editText.getText().toString());
                        editSize = editText.getText().toString().length();
                    }
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        protected void onProgressUpdate(String... values) {
            Log.d("Jun", values[0]);
            super.onProgressUpdate(values);
        }
    }

    @Override
    protected void onDestroy() {
        editAsyncTask = null;
        super.onDestroy();
    }

    public void startIntent() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//调用android的图库
        startActivityForResult(intent, 2);
    }

    private void test(String path) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("token", "BKXZRFVXGSHVHTMTDJVW2OI98FQTGCPO8IGHUO7Y5KXCH0L7HXJUHJHYM1FKIDYX");
        httpParams.put("uid", "95");
        httpParams.put("typr", "car");
        Intent intent = new Intent();
        intent.putExtra("URL", "http://api.app.qiche100.com/order/upfile/option/upimage");
        intent.putExtra("Tag", new File(path).getName());
        intent.putExtra("File", path);
        intent.putExtra("HttpParams",httpParams);
        intent.setClass(this, UploadingActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2){
            if (data == null || data.getData() == null) return;
                    Uri uri = data.getData();
                    String path = GetPathFromUri4kitkat.getPath(this, uri);
//                    Bitmap b = BitmapFactory.decodeFile(path);
            test(path);

        }
    }

}
